package article_new

import (
	"github.com/jinzhu/gorm"
)

type ApaasUsers struct {
	ApaasId    int64 `json:"apaas_id" gorm:"apaas_id"`       // 智能创作云id
	Uid        int64 `json:"uid" gorm:"uid"`                 // 科普中国用户uid
	CreateTime int64 `json:"create_time" gorm:"create_time"` // 创建时间
	IsDel      int8  `json:"is_del" gorm:"is_del"`           // 是否删除 1正常 2删除
	UseNum     int64 `json:"use_num" gorm:"use_num"`         // 账号使用次数
	Id         int64 `json:"id" gorm:"id"`                   // id
}

func (*ApaasUsers) TableName() string {
	return "apaas_users"
}

// ExistTagByName checks if there is a tag with the same name
func ExistTagByName(ApaasId int64) (bool, error, int64) {
	var apaas_users ApaasUsers
	err := db.Table("apaas_users").Select("id,apaas_id").Where("apaas_id = ? AND uid = ? ", ApaasId, 0).First(&apaas_users).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err, 0
	}

	if apaas_users.Id > 0 {
		return true, nil, apaas_users.ApaasId
	}

	return false, nil, 0
}

func CheckUser(userName string, password string) (int64, bool, error) {
	var apaas_users ApaasUsers
	err := db.Table("apaas_users").Select("id,apaas_id").Where("apaas_id = ? AND uid = ? ", 7455789986965913611, 0).First(&apaas_users).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, false, nil
	}

	if apaas_users.Id > 0 {
		return 1, true, nil
	}

	return 0, true, nil
}

func GetUser(apaasId string) (int64, bool, error) {
	var apaas_users ApaasUsers
	err := db.Table("apaas_users").Select("id,apaas_id").Where("apaas_id = ?  ", apaasId).First(&apaas_users).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, false, nil
	}

	if apaas_users.Id > 0 {
		return 1, true, nil
	}

	return apaas_users.Id, true, nil
}
